package com.tbit.uqbike.webmanager.util;

import cn.hutool.http.Header;
import cn.hutool.http.HttpRequest;
import com.alibaba.fastjson.JSONObject;
import com.tbit.uqbike.constant.OperaPswConstant;
import com.tbit.uqbike.object.pojo.AppPushParam;
import lombok.extern.slf4j.Slf4j;

import java.util.Date;
import java.util.Map;

/**
 * 小程序推送工具类
 *
 * @Author:BUGTian
 * @DATE: 2022/7/29
 */
@Slf4j
public class AppPushUtil {
    /**
     * 关锁成功通知
     */
    public static final Integer TEMPLATE_TYPE_LOCK = 0;

    /**
     * 微信推送开关
     */
    public static String ACCOUNTCONFIG_MODEL = "weixin_model";

    /**
     * 发送小程序通知
     *
     * @param accountId 品牌id
     * @param userId    用户id
     * @param map       附加参数
     * @param type      类型
     */
    public static void sendLockMsg(Integer accountId, Integer userId, Map<String, String> map, Integer type) {
        String url = OperaPswConstant.ACCOUNT_OPERA_PUSH_URL + "/weixinPay/sendLockMsg.do";
        JSONObject jsonObjectTemplate;
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("accountId", accountId);
        jsonObject.put("userId", userId);
        jsonObject.put("param", map);
        jsonObject.put("type", type);
        try {
            String data = HttpRequest.post(url)
                    .header(Header.CONTENT_TYPE, "application/json")
                    .header(Header.ACCEPT, "application/json")
                    .timeout(10000)
                    //请求主体
                    .body(JSONObject.toJSONString(jsonObject))
                    .execute().body();
            //结果转成Object对象
            jsonObjectTemplate = JSONObject.parseObject(data);
            log.info("发送消息模板返回参数" + jsonObjectTemplate);
        } catch (Exception e) {
            log.error("获取token异常", e);
        }
    }

    public static void main(String[] args) {
        Date startTime = new Date();
        sendLockMsg(11884, 7779854, new AppPushParam(startTime, new Date(startTime.getTime() + 62000), "欢迎下次再次骑行！").initParam(), TEMPLATE_TYPE_LOCK);
    }
}
